package cn.dxh.stagefinal.service.impl;

import cn.dxh.stagefinal.bean.CommBrand;
import cn.dxh.stagefinal.bean.CommType;
import cn.dxh.stagefinal.bean.Commodity;
import cn.dxh.stagefinal.bean.SalesChannel;
import cn.dxh.stagefinal.config.PageConfig;
import cn.dxh.stagefinal.dao.CommodityDao;
import cn.dxh.stagefinal.service.CommodityService;
import cn.dxh.stagefinal.utils.ExcelUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/12/10 9:11
 *
 * @author Wenhao Lee
 * 心向远方，不负厚望。
 */
@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityDao commodityDao;

    @Override
    public PageInfo<Commodity> findCommodity(Integer pageIndex, String commName, Integer commBrand, Integer commType, Integer state) {
        Map<String, Object> map = new HashMap<>();
        map.put("commName", commName);
        map.put("commBrand", commBrand);
        map.put("commType", commType);
        map.put("state", state);
        PageHelper.startPage(pageIndex, PageConfig.PAGE_SIZE);
        List<Commodity> data = commodityDao.findCommodity(map);
        return new PageInfo<>(data);
    }

    @Override
    public Commodity findCommById(Integer id) {
        return commodityDao.findCommById(id);
    }

    @Override
    @Transactional
    public int updateComm(Commodity commodity, List<Integer> channelIds) {
        System.out.println(commodity.getId());
        //1.先删除全部供应商
        int delete = commodityDao.removeChannelByCommId(commodity.getId());
        System.out.println("删除：" + delete);
        //2.添加新的供应商
        Map<String, Object> map = new HashMap<>();
        map.put("commId", commodity.getId());
        map.put("ids", channelIds);
        int insert = commodityDao.addChannelByCommId(map);
        System.out.println("添加" + insert);
        //3.更新商品信息
        int update = commodityDao.updateComm(commodity);
        System.out.println("更新" + update);
        return insert > 0 && delete > 0 && update > 0 ? 1 : 0;
    }

    @Override
    public List<SalesChannel> findAllChannel() {
        return commodityDao.findAllChannel();
    }

    @Override
    public PageInfo<SalesChannel> findSalesChannel(Integer pageIndex, String channelName) {
        PageHelper.startPage(pageIndex, PageConfig.PAGE_SIZE);
        List<SalesChannel> data = commodityDao.findSalesChannel(channelName);
        return new PageInfo<>(data);
    }

    @Override
    public int deleteChannel(Integer channelId) {
        //1.先删除中间表中数据
        //2.再删除渠道表中数据
        return 0;
    }


    @Override
    public int addChannel(SalesChannel channel) {
        return commodityDao.addChannel(channel);
    }

    @Override
    @Transactional
    public int addComm(Commodity commodity, List<Integer> channelIds) {
        //先查看品牌在库中是否存在
        CommBrand brand = commodityDao.findBrandByName(commodity.getCommBrandStr());
        int bid;
        if (brand == null) {
            //如果不存在则添加，并获取id值
            CommBrand insertBrand = new CommBrand(null, commodity.getCommBrandStr());
            commodityDao.addCommBrand(insertBrand);
            bid = insertBrand.getBid();
        } else {
            //如果存在，直接获取ID值
            bid = brand.getBid();
        }
        commodity.setCommBrand(bid);
        //再查看商品类型是否存在
        CommType type = commodityDao.findTypeByName(commodity.getCommTypeStr());
        int tid;
        if (type == null) {
            //如果不存在，则在数据库中添加，并获取ID
            CommType insertType = new CommType(null, commodity.getCommTypeStr());
            commodityDao.addCommType(insertType);
            tid = insertType.getTid();
        } else {
            //如果存在，把ID直接拿来用
            tid = type.getTid();
        }
        commodity.setCommType(tid);
        int insertComm = commodityDao.addComm(commodity);
        Map<String, Object> map = new HashMap<>();
        map.put("commId", commodity.getId());
        map.put("ids", channelIds);
        int insertChannel = commodityDao.addChannelByCommId(map);
        return insertComm > 0 && insertChannel > 0 ? 1 : 0;
    }

    @Override
    public String exportToExcel(List<Integer> ids) {
        List<Commodity> data = commodityDao.findByIds(ids);
        if (data == null) {
            return null;
        }
        String name = "Commodity" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String path = "d:\\javaCreate\\" + name + ".xls";
        try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path))) {
            ExcelUtils<Commodity> utils = new ExcelUtils<>();
            utils.export(data, bos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    @Override
    public List<CommType> findAllType() {
        return commodityDao.findAllType();
    }

    @Override
    public List<CommBrand> findAllBrand() {
        return commodityDao.findAllBrand();
    }
}
